package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.code.petapp.bean.FosterBean;
import com.code.petapp.databinding.ActivityPostFosterBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.FosterUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.Calendar;

public class PostFosterActivity extends BaseActivity {
    private ActivityPostFosterBinding binding;
    private Calendar calendar = Calendar.getInstance();

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPostFosterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setCalendar();
        binding.post.setOnClickListener(view -> {
            if(binding.name.getText().toString().isEmpty()||binding.time.getText().toString().isEmpty()||binding.days.getText().toString().isEmpty()){
                toastShortShow("请完善信息");
            }else{
                setPost();
            }
        });

    }

    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    private void setCalendar() {
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        binding.calendarView.setOnPageChangedCallBack((year, month) -> {
            binding.calendarTitle.setText(year + "-" + month);
        });
        binding.calendarView.setOnDateSelectCallBack((year, month, day) -> {
            if (year < currentYear ||
                    (year == currentYear && month < currentMonth) ||
                    (year == currentYear && month == currentMonth && day < currentDay)) {
                // 如果选择的日期无效，可以给出提示
                toastShortShow("无法选择当前日期之前的日期");
                binding.time.setText("");
                return;
            }
            binding.time.setText(String.format("%04d-%02d-%02d", year, month, day));
        });
    }

    private void setPost(){
        FosterBean fosterBean=new FosterBean();
        fosterBean.setName(binding.name.getText().toString());
        fosterBean.setTime(binding.time.getText().toString());
        fosterBean.setDays(binding.days.getText().toString());
        fosterBean.setUserId(SPCommonUtils.getInstance().getUserId());
        FosterUtils.add(SPCommonUtils.getInstance().getToken(),fosterBean, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(String data) {
                toastShortShow("发布成功");
                startActivity(new Intent(PostFosterActivity.this,FosterActivity.class));
                finish();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
}